using System;
using System.Collections;
using BitMango;
using BitMango.Diagnostics;
using BMCommon;
using UnityEngine;

public class AttendPopupHandler : MonoBehaviour, IPopupWnd, IHandlerBase
{
	public void DoDataExchange()
	{
	}

	public void OnEnterPopup(object param)
	{
		this.isGetReward = false;
		this.confirmButton.SetActive(true);
		PlayerPrefs.SetString("REWARD_CHECK", "OK");
		PlayerPrefs.Save();
		this.coloredPapaer.SetActive(false);
		if (PlayerPrefs.GetString("REWARD_START", "NONE").Equals("NONE"))
		{
			PlayerPrefs.SetString("REWARD_START", DateTime.Now.ToString());
			PlayerPrefs.Save();
		}
		string @string = PlayerPrefs.GetString("REWARD_START", "NONE");
		TimeSpan timeSpan = DateTime.Now - DateTime.Parse(@string);
		for (int i = 0; i < 7; i++)
		{
			if (i == timeSpan.Days)
			{
				this.todayIndex = i;
				this.SetAttendUI(i, AttendPopupHandler.STATUS.TODAY);
			}
			else if (i < timeSpan.Days && PlayerPrefs.GetString("REWARD_" + i, "NONE").Equals("NONE"))
			{
				this.SetAttendUI(i, AttendPopupHandler.STATUS.FAIL);
			}
			else if (i > timeSpan.Days)
			{
				this.SetAttendUI(i, AttendPopupHandler.STATUS.FUTURE);
			}
			else if (i < timeSpan.Days && !PlayerPrefs.GetString("REWARD_" + i, "NONE").Equals("NONE"))
			{
				this.SetAttendUI(i, AttendPopupHandler.STATUS.PAST);
			}
		}
	}

	private void RewardCoin(int day)
	{
		Debugger.Assert(day >= 0, "AttendPopupHandler day >= 0");
		this.reward = 0;
		switch (day)
		{
		case 0:
			this.reward = 25;
			break;
		case 1:
			this.reward = 25;
			break;
		case 2:
			this.reward = 50;
			break;
		case 3:
			this.reward = 50;
			break;
		case 4:
			this.reward = 50;
			break;
		case 5:
			this.reward = 50;
			break;
		case 6:
			this.reward = 100;
			break;
		}
		Wallet.Gain("blockcoin", this.reward, "SEVEN_REWARD");
		Wallet.Save();
		EconomyLog.SendEconomyLog(LOG_FROM.ATTEND_REWARD, LOG_TYPE.CONSUMPTION, "blockcoin", this.reward);
		PlayerPrefs.SetString("REWARD_TIME", DateTime.Now.ToString());
		PlayerPrefs.SetString("REWARD_" + day, "REWARD");
		PlayerPrefs.Save();
	}

	private void SetAttendUI(int index, AttendPopupHandler.STATUS status)
	{
		Debugger.Assert(index >= 0, "AttendPopupHandler index >= 0");
		tk2dSprite component = this.daily[index].transform.Find("check").gameObject.GetComponent<tk2dSprite>();
		if (status == AttendPopupHandler.STATUS.TODAY)
		{
			component.spriteName = "icon_success";
			this.daily[index].transform.Find("today").gameObject.SetActive(true);
			this.daily[index].transform.Find("check").gameObject.SetActive(false);
			this.daily[index].transform.Find("curtain").gameObject.SetActive(false);
		}
		else if (status == AttendPopupHandler.STATUS.PAST)
		{
			this.daily[index].transform.Find("today").gameObject.SetActive(false);
			this.daily[index].transform.Find("curtain").gameObject.SetActive(true);
			component.gameObject.SetActive(true);
			component.spriteName = "icon_success";
		}
		else if (status == AttendPopupHandler.STATUS.FUTURE)
		{
			this.daily[index].transform.Find("today").gameObject.SetActive(false);
			this.daily[index].transform.Find("curtain").gameObject.SetActive(false);
			component.gameObject.SetActive(false);
		}
		else if (status == AttendPopupHandler.STATUS.FAIL)
		{
			this.daily[index].transform.Find("today").gameObject.SetActive(false);
			this.daily[index].transform.Find("curtain").gameObject.SetActive(false);
			component.gameObject.SetActive(true);
			component.spriteName = "icon_fail";
		}
	}

	private IEnumerator CoDailyReward(int index, Action callback = null)
	{
		Debugger.Assert(index >= 0, "AttendPopupHandler index >= 0");
		this.coloredPapaer.SetActive(true);
		BMSoundHelper.Play("se_dailyreward", false, 1f);
		yield return new WaitForSeconds(0.1f);
		this.daily[index].transform.Find("check").gameObject.transform.localScale = Vector3.zero;
		this.daily[index].transform.Find("check").gameObject.Popup(0.5f);
		yield return new WaitForSeconds(0.7f);
		if (callback != null)
		{
			callback();
		}
		Scene.ClosePopup("AttendPopup", null);
		yield break;
	}

	public void OnLeavePopup()
	{
	}

	private void OnClosePopup()
	{
		BMSoundHelper.Play("se_btnclick", false, 1f);
		this.isGetReward = true;
		this.confirmButton.SetActive(false);
		this.RewardCoin(this.todayIndex);
		GameObject gameObject = this.daily[this.todayIndex].transform.Find("coin").gameObject;
		GameObject gameObject2 = gameObject.transform.Find("icon").gameObject;
		this.coinInfo.CoinAnimation(this.cam.transform.InverseTransformPoint(gameObject2.transform.position), 5, 0.15f, this.reward, delegate
		{
			base.StartCoroutine(this.CoDailyReward(this.todayIndex, null));
		});
	}

	private void OnEscape()
	{
		if (!this.isGetReward)
		{
			this.OnClosePopup();
		}
	}

	[SerializeField]
	private tk2dCamera cam;

	[SerializeField]
	private GameObject curtain;

	public GameObject[] daily;

	public GameObject coloredPapaer;

	public GameObject confirmButton;

	public TopOverlayHandler coinInfo;

	private int todayIndex;

	private int reward;

	private bool isGetReward;

	private enum STATUS
	{
		PAST,
		TODAY,
		FUTURE,
		FAIL
	}
}
